home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / SMALLTAL / ROTATING._BI < prev    next >
Text File  |  1990-07-16  |  6KB  |  158 lines

  1.  
  2. Anybody tried to rotate bit-maps by 45 degrees?
  3. If you are interested and see a use for it, here is a simple
  4. implementation. If you find a better/faster way of doing
  5. it, please post it.
  6.  
  7. -------------------------- cut here ------------------------
  8. 'These methods allow you to rotate Forms by 45 degrees.
  9. The other angles (135, 225, 315) can easily be implemented.
  10. The lazy way would be to rotate the first by 90, 180 or 270.
  11. See Form>>rotate45 for more information on its use.
  12.  
  13. Try:    Cursor blank showWhile:
  14.                         [(Cursor thumbsUp rotate45 shiftBy: 1@0)
  15.                                 follow: [Sensor cursorPoint]
  16.                                 while: [Sensor noButtonPressed]]
  17.  
  18. I guess it usefull if you want your PC to hitch-hike,
  19. have fun, Pieter.'!
  20.  
  21. !Form methodsFor: 'private'!
  22.  
  23. rotate45Square
  24.         "Rotate a square bitmap by 45 degrees.
  25.         A bitmap of 2 by 2 pixels is mapped a bitmap of 3 by 3:
  26.  
  27.                 12              x1x
  28.                 34      ->      3x2
  29.                                 x4x     
  30.         x indicates a -white- pixel.
  31.  
  32.         See Form>>rotate45 for more information.
  33.         Written by Pieter S. van der Meulen."
  34.  
  35.         | newForm w2 w2Point destForm newBitBlt destBitBlt | 
  36.         (width > 2 or: [height > 2])
  37.                 ifFalse:
  38.                         [^Form
  39.                                 extent: 3@3
  40.                                 fromArray: (Array
  41.                                         with: (((bits at: 1) bitAnd: 32768) bitShift: -1)
  42.                                         with: ((((bits at: 1) bitAnd: 16384) bitShift: -1)
  43.                                                 bitOr: ((bits at: 2) bitAnd: 32768))
  44.                                         with: ((bits at: 2) bitAnd: 16384))
  45.                                 offset: 0@0].
  46.         newForm _ Form extent: (width * 2 - 1) asPoint.
  47.         w2 _ width / 2.
  48.         w2Point _ w2 asPoint.
  49.         destForm _ self class extent: w2Point.
  50.         destBitBlt _ (BitBlt 
  51.                 destForm: destForm
  52.                 sourceForm: self
  53.                 halftoneForm: nil
  54.                 combinationRule: Form over
  55.                 destOrigin: 0@0
  56.                 sourceOrigin: 0@0
  57.                 extent: w2Point
  58.                 clipRect: (0@0 extent: w2Point)).
  59.         destBitBlt copyBits.
  60.         newBitBlt _ BitBlt 
  61.                 destForm: newForm
  62.                 sourceForm: destForm rotate45Square
  63.                 halftoneForm: nil
  64.                 combinationRule: Form under
  65.                 destOrigin: w2@0
  66.                 sourceOrigin: 0@0
  67.                 extent: self extent
  68.                 clipRect: newForm boundingBox.
  69.         newBitBlt copyBits.
  70.  
  71.         destBitBlt sourceOrigin: 0@w2; copyBits.
  72.         newBitBlt
  73.                 sourceForm: destForm rotate45Square;
  74.                 destOrigin: 0@w2; copyBits.
  75.  
  76.         destBitBlt sourceOrigin: w2@0; copyBits.
  77.         newBitBlt
  78.                 sourceForm: destForm rotate45Square;
  79.                 destOrigin: width@w2; copyBits.
  80.  
  81.         destBitBlt sourceOrigin: w2Point; copyBits.
  82.         newBitBlt
  83.                 sourceForm: destForm rotate45Square;
  84.                 destOrigin: w2@width; copyBits.
  85.         ^newForm! !
  86.  
  87. !Form methodsFor: 'image manipulation'!
  88.  
  89. rotate45
  90.         "Rotate a Form by 45 degrees. Be patient, it is a compute-intensive method.
  91.         Forms are first converted to a rectangular form and then rotated, so a
  92.         100X1 form may be just as slow to rotate as a 100X100 form.
  93.         This method enlarges your original by about 1.4 (2 sqrt), but does not -loose-
  94.         any bits. You may use the <shiftBy: 1@0> method on the result to make it solid.
  95.  
  96.         The original size is almost obtained (but quality is not) if you magnify by (2/3) :
  97.                 (((Form fromUser rotate45 shiftBy: 1@1)
  98.                                 magnifyBy: 2@2) shrinkBy: 3@3) display.
  99.  
  100.         The best result however is obtained if you only shift the result, try:
  101.                 (Form fromUser rotate45 shiftBy: 1@0) display.
  102.         Written by Pieter S. van der Meulen."
  103.  
  104.         | rotSize rotForm | 
  105.         rotSize _ width max: height.
  106.         rotSize _ 2 raisedTo: ((rotSize-1) asFloat floorLog: 2)+1.
  107.         rotForm _ self class extent: rotSize asPoint.
  108.         (BitBlt 
  109.                 destForm: rotForm
  110.                 sourceForm: self
  111.                 halftoneForm: nil
  112.                 combinationRule: 3
  113.                 destOrigin: 0@0
  114.                 sourceOrigin: 0@0
  115.                 extent: rotSize asPoint
  116.                 clipRect: (0@0 extent: width@height)) copyBits.
  117.         ^rotForm rotate45Square!
  118.  
  119. shiftBy: aPoint 
  120.         "The new form will be aPoint larger than myself. Lines will be aPoint bigger."
  121.  
  122.         | saveOffset shiftedForm box x y |
  123.         saveOffset _ self offset.
  124.         self offset: 0 @ 0.
  125.         shiftedForm _ Form new extent: (self extent + aPoint).
  126.         box _ shiftedForm boundingBox.
  127.         x _ aPoint x.
  128.         [x >= 0]
  129.                 whileTrue:
  130.                         [y _ aPoint y.
  131.                         [y >= 0]
  132.                                 whileTrue:
  133.                                         [shiftedForm
  134.                                                 copyBits: box
  135.                                                 from: self
  136.                                                 at: (x @ y)
  137.                                                 clippingBox: box
  138.                                                 rule: Form under
  139.                                                 mask: nil.
  140.                                                 y _ y - 1].
  141.                         x _ x - 1].
  142.         self offset: saveOffset.
  143.         shiftedForm offset: offset + (aPoint // 2).
  144.         ^shiftedForm!
  145.  
  146. shiftShrinkBy: aPoint 
  147.         "Return aForm first shifted and then shrunk by aPoint 
  148.         factor. "
  149.  
  150.         aPoint x > 1 | (aPoint y > 1) ifFalse: [^self].
  151.         ^(self shiftBy: aPoint - (1 @ 1)) shrinkBy: aPoint! !
  152. -------------------------- cut here ------------------------
  153. -- 
  154. ---------------------------------------------
  155. P.S. van der Meulen, MS 02        prls!pieter
  156. PRLS, Signetics div. of NAPC      -----------
  157. 811 E.Arques Avenue, Sunnyvale, CA 94088-3409
  158.